#include "StdAfx.h"
#include "DataManager.h"
#include "Utils.h"
#include <algorithm>

DataManager* DataManager::m_pInstance = nullptr;
vector<CdrModel*> DataManager::m_vtRecordCdr;

DataManager::DataManager(void)
{
}

DataManager::~DataManager(void)
{
	clearRecordCdr();

	vector<CWeekPlanModel*>::iterator weekIt = m_vtWeekPlan.begin();
	for (; weekIt != m_vtWeekPlan.end(); ++weekIt)
	{
		SAFE_RELEASE(*weekIt);
	}
	m_vtWeekPlan.clear();

	vector<CMonthPlanModel*>::iterator monthIt = m_vtMonthPlan.begin();
	for (; monthIt != m_vtMonthPlan.end(); ++monthIt)
	{
		SAFE_RELEASE(*monthIt);
	}
	m_vtMonthPlan.clear();

	vector<CityDistrictModel*>::iterator districtIt = m_vtCityDistrict.begin();
	for (; districtIt != m_vtCityDistrict.end(); ++districtIt)
	{
		SAFE_RELEASE(*districtIt);
	}
	m_vtCityDistrict.clear();

	vector<LendingAgencyModel*>::iterator agencyIt = m_vtLendingAgency.begin();
	for (; agencyIt != m_vtLendingAgency.end(); ++agencyIt)
	{
		SAFE_RELEASE(*agencyIt);
	}
	m_vtLendingAgency.clear();

	map<int, vector<CWeekPlanModel*>* >::iterator beforePlanIt = m_vtBeforeWeekPlanMap.begin();
	for (; beforePlanIt != m_vtBeforeWeekPlanMap.end(); beforePlanIt++)
	{
		vector<CWeekPlanModel*>* pvtPlan = beforePlanIt->second;
		vector<CWeekPlanModel*>::iterator vtPlanIt = pvtPlan->begin();
		for (; vtPlanIt != pvtPlan->end(); vtPlanIt++)
		{
			SAFE_RELEASE(*vtPlanIt);
		}
		SAFE_RELEASE(pvtPlan);
	}
	m_vtBeforeWeekPlanMap.clear();

	map<CString, vector<StaffDayKPIModel*>* >::iterator mapIt = m_mapDayKpi.begin();
	for (; mapIt != m_mapDayKpi.end(); mapIt++)
	{
		vector<StaffDayKPIModel*>* pvtDayKpi = mapIt->second;
		vector<StaffDayKPIModel*>::iterator vtDayIt = pvtDayKpi->begin();
		for (; vtDayIt != pvtDayKpi->end(); vtDayIt++)
		{
			SAFE_RELEASE(*vtDayIt);
		}
		SAFE_RELEASE(pvtDayKpi);
	}
	m_mapDayKpi.clear();

}

void DataManager::clearRecordCdr()
{
	vector<CdrModel*>::iterator vtIt = m_vtRecordCdr.begin();
	for (; vtIt != m_vtRecordCdr.end(); ++vtIt)
	{
		SAFE_RELEASE(*vtIt);
	}
	m_vtRecordCdr.clear();
}

bool DataManager::hasKpiData(const CString& strDay, vector<StaffDayKPIModel*>*& dayKpiData)
{
	map<CString, vector<StaffDayKPIModel*>* >::iterator mapIt = m_mapDayKpi.find(strDay);
	if (mapIt != m_mapDayKpi.end())
	{		
		dayKpiData = mapIt->second;
		return true;
	}

	return false;
}

void DataManager::clearDayKpi(const CString& strDay)
{
	map<CString, vector<StaffDayKPIModel*>* >::iterator mapIt = m_mapDayKpi.find(strDay);
	if (mapIt != m_mapDayKpi.end())
	{
		vector<StaffDayKPIModel*>* pvtDayKpi = mapIt->second;
		vector<StaffDayKPIModel*>::iterator vtDayIt = pvtDayKpi->begin();
		for (; vtDayIt != pvtDayKpi->end(); vtDayIt++)
		{
			SAFE_RELEASE(*vtDayIt);
		}
		SAFE_RELEASE(pvtDayKpi);
		m_mapDayKpi.erase(mapIt);
	}
}

void DataManager::insertDayKpi(const CString& strDay, vector<StaffDayKPIModel*>* dayKpiData)
{
	map<CString, vector<StaffDayKPIModel*>* >::iterator existIt = m_mapDayKpi.find(strDay);
	if (existIt == m_mapDayKpi.end())
	{
		m_mapDayKpi.insert(make_pair(strDay, dayKpiData));
	} 
	else
	{
		clearDayKpi(strDay);
		m_mapDayKpi[strDay] = dayKpiData;
	}
}

CWeekPlanModel* DataManager::getWeekPlanByUserID(const int userID)
{
	vector<CWeekPlanModel*>::iterator weekIt = m_vtWeekPlan.begin();
	for (; weekIt != m_vtWeekPlan.end(); ++weekIt)
	{
		CWeekPlanModel* pWeekPlan = *weekIt;
		if (pWeekPlan->m_userID == userID)
		{
			return pWeekPlan;
		}
	}

	return nullptr;
}

CMonthPlanModel* DataManager::getMonthPlanByUserID(const int userID)
{
	vector<CMonthPlanModel*>::iterator monthIt = m_vtMonthPlan.begin();
	for (; monthIt != m_vtMonthPlan.end(); ++monthIt)
	{
		CMonthPlanModel* pMonthPlan = *monthIt;
		if (pMonthPlan->m_userID == userID)
		{
			return pMonthPlan;
		}
	}

	return nullptr;
}

void DataManager::updateUserWeekPlan(CWeekPlanModel* pWeekPlan)
{
	if (pWeekPlan == nullptr)
	{
		return;
	}
	int userID = pWeekPlan->m_userID;
	vector<CWeekPlanModel*>::iterator weekIt = m_vtWeekPlan.begin();
	for (; weekIt != m_vtWeekPlan.end(); ++weekIt)
	{
		CWeekPlanModel* pWeekPlan = *weekIt;
		if (pWeekPlan->m_userID == userID)
		{
			SAFE_RELEASE(pWeekPlan);
			m_vtWeekPlan.erase(weekIt);
			break;
		}
	}
	m_vtWeekPlan.push_back(pWeekPlan);
}

void DataManager::updateUserMonthPlan(CMonthPlanModel* pMonthPlan)
{
	if (pMonthPlan == nullptr)
	{
		return;
	}
	int userID = pMonthPlan->m_userID;
	vector<CMonthPlanModel*>::iterator monthIt = m_vtMonthPlan.begin();
	for (; monthIt != m_vtMonthPlan.end(); ++monthIt)
	{
		CMonthPlanModel* pMonthPlan = *monthIt;
		if (pMonthPlan->m_userID == userID)
		{
			SAFE_RELEASE(pMonthPlan);
			m_vtMonthPlan.erase(monthIt);
			break;
		}
	}
	m_vtMonthPlan.push_back(pMonthPlan);
}

void DataManager::cleanMonthPlanData(CString& strMonth) 
{
	vector<CMonthPlanModel*>::iterator monthIt = m_vtMonthPlan.begin();
	for (; monthIt != m_vtMonthPlan.end(); ++monthIt)
	{
		SAFE_RELEASE(*monthIt);
	}
	m_vtMonthPlan.clear();
}

void DataManager::addBeforeWeekPlan(const int beforeWeek, CWeekPlanModel* pPlan)
{
	map<int, vector<CWeekPlanModel*>* >::iterator beforeIt = m_vtBeforeWeekPlanMap.find(beforeWeek);
	vector<CWeekPlanModel*>* beforeWeekPlan = nullptr;
	if (beforeIt == m_vtBeforeWeekPlanMap.end())
	{
		beforeWeekPlan = new vector<CWeekPlanModel*>();
		m_vtBeforeWeekPlanMap.insert(make_pair(beforeWeek, beforeWeekPlan));
	}
	else 
	{
		beforeWeekPlan = beforeIt->second;
	}
	beforeWeekPlan->push_back(pPlan);
}

CWeekPlanModel* DataManager::getBeforeWeekPlanByUserID(const int userID, const int beforeWeek)
{
	map<int, vector<CWeekPlanModel*>* >::iterator beforeIt = m_vtBeforeWeekPlanMap.find(beforeWeek);
	if (beforeIt == m_vtBeforeWeekPlanMap.end())
	{
		return nullptr;
	}
	vector<CWeekPlanModel*>* beforeWeekPlan = beforeIt->second;
	vector<CWeekPlanModel*>::iterator planIt = beforeWeekPlan->begin();
	for (; planIt != beforeWeekPlan->end(); planIt++)
	{
		CWeekPlanModel* pPlan = *planIt;
		if (pPlan->m_userID == userID)
		{
			return pPlan;
		}
	}

	return nullptr;
}

bool DataManager::hasBreforeWeekPlan(const int beforeWeek)
{
	map<int, vector<CWeekPlanModel*>* >::iterator beforeIt = m_vtBeforeWeekPlanMap.find(beforeWeek);
	if (beforeIt == m_vtBeforeWeekPlanMap.end())
	{
		return false;
	}

	return true;
}

void DataManager::deleteCityDistrictByID(const int id)
{
	vector<CityDistrictModel*>::iterator districtIt = m_vtCityDistrict.begin();
	for (; districtIt != m_vtCityDistrict.end(); ++districtIt)
	{
		CityDistrictModel* pDistrict = *districtIt;
		if (pDistrict->m_id == id)
		{
			SAFE_RELEASE(pDistrict);
			m_vtCityDistrict.erase(districtIt);
			break;
		}
	}
}

void DataManager::deleteLendingAgencyByID(const int id)
{
	vector<LendingAgencyModel*>::iterator agencyIt = m_vtLendingAgency.begin();
	for (; agencyIt != m_vtLendingAgency.end(); ++agencyIt)
	{
		LendingAgencyModel* pAgency = *agencyIt;
		if (pAgency->m_id == id)
		{
			SAFE_RELEASE(pAgency);
			m_vtLendingAgency.erase(agencyIt);
			break;
		}
	}
}

void DataManager::updateSipConfig(CSipConfigModel* pSipConfig)
{
	bool isExist = false;
	vector<CSipConfigModel*>::iterator vtIt = m_vtSipConfig.begin();
	for (; vtIt != m_vtSipConfig.end(); ++vtIt)
	{
		CSipConfigModel* poldSip = *vtIt;
		if (pSipConfig->m_companyID == poldSip->m_companyID && 
			pSipConfig->m_cityName.Compare(poldSip->m_cityName) == 0)
		{
			isExist = true;
			SAFE_RELEASE(poldSip);
			m_vtSipConfig.erase(vtIt);
			m_vtSipConfig.push_back(pSipConfig);
			break;
		}
	}
	if (!isExist)
	{
		m_vtSipConfig.push_back(pSipConfig);
	}
}

CSipConfigModel* DataManager::getSipConfigByCompanyID(int companyID)
{
	vector<CSipConfigModel*>::iterator vtIt = m_vtSipConfig.begin();
	for (; vtIt != m_vtSipConfig.end(); ++vtIt)
	{
		CSipConfigModel* poldSip = *vtIt;
		if (poldSip->m_companyID == companyID)
		{
			return poldSip;
		}
	}

	return nullptr;
}

void DataManager::onEventRemind()
{
	if (m_vtEventRemind.size() == 0)
	{
		return;
	}



}




